package com.ax.base.drugstore.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ax.base.common.domain.Tree;
import com.ax.base.common.utils.AjaxRes;
import com.ax.base.common.utils.Const;
import com.ax.base.drugstore.domain.BaseDrugstore;
import com.ax.base.drugstore.service.DrugstoreService;
import com.ax.base.system.domain.MenuDO;

@Controller
@RequestMapping("/sys/drugstore")
public class DrugstoreController {

	@Autowired
	DrugstoreService drugstoreService;
	
	/**
	 * 菜单页面的跳转
	 */
	@GetMapping()
	String drugAdministration(Model model) {
		return "drugstore/store/store";
	}
	
	/**
	 * 药房查询(模块:药店/公司信息)
	 * 
	 * @param Map<String, Object> map
	 *           
	 * @return List<BaseDrugstore>
	 */
	@RequestMapping("/queryDrugStore")
	@ResponseBody
	public List<BaseDrugstore> queryDrugStore(@RequestParam Map<String, Object> params) {
		List<BaseDrugstore> drugAdministrations = this.drugstoreService.queryDrugStoreSer(params);
		return drugAdministrations;
	  }
	
	/**
	 * 药房增加(模块:药房管理)*跳转增加页面
	 * 
	 * @param Long pId
	 *           
	 * @return BaseDrugstore BaseDrugstore
	 */
	@GetMapping("/add/{pId}")
	String add(Model model, @PathVariable("pId") Long pId) {
		model.addAttribute("pId", pId);
		if (pId == 0) {
			model.addAttribute("pName", "根目录");
		} else {
			model.addAttribute("pName",drugstoreService.getBaseDrugstore(pId).getName());
		}
		return "drugstore/store/add";
	}
	
	/**
	 * 药房保存(模块:药房管理)
	 * 
	 * @param BaseDrugstore baseDrugstore
	 *           
	 * @return SUCC
	 */
	@RequestMapping("/insertBaseDrugstore")
	@ResponseBody
	public AjaxRes insertBaseDrugstore(AjaxRes ajaxRes,BaseDrugstore baseDrugstore){
		try {
			this.drugstoreService.insertBaseDrugstoreSer(baseDrugstore);
			ajaxRes.setSucceed(Const.SAVE_SUCCEED);
		} catch (Exception e){
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}
	
	/**
	 * 药房修改(模块:药房管理)*跳转修改页面
	 * 
	 * @param Long id
	 *           
	 * @return BaseDrugstore baseDrugstore
	 */
	@GetMapping("/edit/{id}")
	String edit(Model model, @PathVariable("id") Long id) {
		model.addAttribute("store",drugstoreService.getBaseDrugstore(id));
		return "drugstore/store/edit";
	}
	
	/**
	 * 药房修改(模块:药房管理)
	 * 
	 * @param BaseDrugstore baseDrugstore
	 *           
	 * @return SUCC
	 */
	@RequestMapping("/updateBaseDrugstore")
	@ResponseBody
	public AjaxRes updateBaseDrugstore(AjaxRes ajaxRes,BaseDrugstore baseDrugstore){
		try {
			this.drugstoreService.updateBaseDrugstoreSer(baseDrugstore);
			ajaxRes.setSucceed(Const.UPDATE_SUCCEED);
		} catch (Exception e){
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}
	
	
	/**
	 * 药店名是否重复(新增)
	 * 
	 * @param String parameter
	 *         
	 * @return boolean
	 */
	@PostMapping("/drugstoreNameValidate")
	@ResponseBody
	public boolean drugstoreNameValidate(String parameter){
		return !drugstoreService.drugstoreNameValidateSer(parameter);
	}
	
	/** Tree
	 * 
	 * @param Long parentDrugstoreId
	 *           
	 * @return Tree<MenuDO>
	 */
	@GetMapping("/tree/{parentDrugstoreId}")
	@ResponseBody
	Tree<MenuDO> tree(@PathVariable("parentDrugstoreId") Long parentDrugstoreId) {
		Tree<MenuDO> tree = drugstoreService.getDrugTree(parentDrugstoreId);
		return tree;
	}
	 
	/**
	 * 药店名是否重复(修改)
	 * 
	 * @param String parameter
	 *         
	 * @return boolean
	 */
	@PostMapping("/updateDrugstoreNameValidate")
	@ResponseBody
	public boolean updateDrugstoreNameValidate(String parameter){
		return !drugstoreService.updateDrugstoreNameValidateSer(parameter);
	}
	
	/**
	 * 代码是否重复(新增)
	 * 
	 * @param Map<String,Object> map
	 *         
	 * @return boolean
	 */
	@PostMapping("/creditCodeValidate")
	@ResponseBody
	public boolean creditCodeValidate(@RequestParam Map<String,Object> map){
		return !drugstoreService.creditCodeValidateSer(map);
	}
	
	/**
	 * 代码是否重复(修改)
	 * 
	 * @param Map<String,Object> map
	 *         
	 * @return boolean
	 */
	@PostMapping("/updateCreditCodeValidate")
	@ResponseBody
	public boolean updateCreditCodeValidate(String parameter){
		return !drugstoreService.updateCreditCodeValidateSer(parameter);
	}
	
	
	
	@RequestMapping("/getTest")
	@ResponseBody
	public AjaxRes getTest(AjaxRes ajaxRes,@RequestParam Map<String,Object> map){
		try {
	//		this.drugstoreService.updateBaseDrugstoreSer(baseDrugstore);
			ajaxRes.setSucceed(Const.UPDATE_SUCCEED);
		} catch (Exception e){
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}
}
